<?php
/**
 * 评论模型
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-07-01
 */
namespace app\common\model;
class CommentModel extends BaseModel {
	private $table_name = 'Comment';
	
	/**
	 * 获取评论列表 分页
	 * @param unknown $result
	 */
	public function getCommentList($result){
		$order = "id desc";
		$join = [
				[config('database')['prefix'].'member m','m.id=c.member_id'],
				[config('database')['prefix'].'article a','a.id=c.article_id'],
		];
		$list = $this->join_table_by_page($this->table_name,'c',$join,$result['condition'],'c.*,m.nick_name,a.article_title',$order,$result['query']);
		return $list;
	}
	/**
	 * 获取评论列表，无分页
	 */
	public function getList($condition=array(),$page = 0){
	    $order = "id desc";
		$join = [
				[config('database')['prefix'].'member m','m.id=c.member_id'],
		];
		$list = $this->join_by_page($this->table_name,'c',$join,$condition,'c.*,m.nick_name,m.head_imgurl',$page,10,$order);
		foreach ($list as $k=>$v){
			$id = $v['id'];
			$article_id = $v['article_id'];
			$list[$k]['child_list'] = $this->getNextComment($id,$article_id);
		}
		return $list;
	}
	/*
	 * 获取下级评论信息
	 */
	public function getNextComment($id,$article_id){
		$result = array();
		$condition = "c.article_id=".$article_id." and c.status = 1 and c.type = 1 and c.is_del = 0 and pid = ".$id;
		$order = "id desc";
		$join = [
				[config('database')['prefix'].'member m','m.id=c.member_id'],
		];
		$i = 0;
		$list = $this->join_table($this->table_name,'c',$join,$condition,'c.*,m.nick_name,m.head_imgurl',$order);
		if(!empty($list)){
			foreach ($list as $k=>$v){
				//获取被回复用户名
				$pid = $v['pid'];
				$v['reply_name'] = $this->getCommentMemberName($pid);
				$result[$i] = $v;
				$i = $i + 1;
				$id = $v['id'];
				$article_id = $v['article_id'];
				$c_list = $this->getNextComment($id,$article_id);
				if(!empty($c_list)){
					foreach($c_list as $ck=>$cv){
						$result[$i] = $cv;
						$i = $i + 1;
					}
				}
			}
		}
		//评论按时间升序排序
		uasort($result,'comment_sort');
		return $result;
	}
	/**
	 * 获取评论条数
	 * @param string $condition
	 */
	public function getCount($condition=array()){
		return $this->count_table($this->table_name,$condition);
	}
	/**
	 * 获取评论列表，去除条件评论
	 */
	public function getRemoveList($tag_list){
		$list = $this->select_table($this->table_name,array(),'*','id asc');
		$result = array();
		$i = 0;
		foreach ($list as $k=>$v){
			$flag = true;
			foreach ($tag_list as $tk=>$tv){
				if($tv['id'] == $v['id']){
					$flag = false;
				}
			}
			if($flag){
				$result[$i] = $v;
				$i++;
			}
		}
		return $result;
	}
	/**
	 * 添加评论
	 */
	public function insertData($data){
	    return $this->add_table($this->table_name,$data,1);
	}
    /**
     * 获取单个评论信息
     * @param unknown $condition
     */
	public function getInfo($condition,$field="*"){
		return $this->find_table($this->table_name,$condition,$field);
	}
	/**
	 * 审核
	 * @param unknown $condition
	 */
	public function auditData($condition){
		$info = $this->getInfo($condition);
		$data = array();
		if($info['status'] == 1){
			$data['status'] = 0;
		}else{
			$data['status'] = 1;
		}
		$res = $this->update_table($this->table_name,$condition,$data,0);
		if($res !== false){
			return $res;
		}else{
			return false;
		}
	}
	/**
	 * 删除评论信息
	 */
	public function deleteData($condition){
		$res = $this->update_table($this->table_name,$condition,array('is_del'=>1),0);
		if($res !== false){
			return $res;
		}else{
			return false;
		}
	}
	/**
	 * 通过评论编号获取用户昵称
	 * @param int $comment_id
	 */
	public function getCommentMemberName($comment_id){
		$comment_info = $this->find_table($this->table_name,array('id'=>$comment_id),'member_id');
		$member_info =  model('Member')->getInfo(array('id'=>$comment_info['member_id']),'nick_name');
		return $member_info['nick_name'];
	}
}
